package org.diffshop.coupon.service.impl;

import org.diffshop.common.to.SkuFullReductionTO;
import org.diffshop.coupon.entity.SmsSkuLadderEntity;
import org.diffshop.coupon.service.MemberPriceService;
import org.diffshop.coupon.service.SmsSkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;

import org.diffshop.coupon.dao.SmsSkuFullReductionDao;
import org.diffshop.coupon.entity.SmsSkuFullReductionEntity;
import org.diffshop.coupon.service.SmsSkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("smsSkuFullReductionService")
public class SmsSkuFullReductionServiceImpl extends ServiceImpl<SmsSkuFullReductionDao, SmsSkuFullReductionEntity> implements SmsSkuFullReductionService {

    @Autowired
    private SmsSkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSkuFullReductionEntity> page = this.page(
                new Query<SmsSkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SmsSkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku满减优惠信息、满折优惠信息、会员价格优惠信息
     *
     * @param skuFullReductionTO
     */
    @Override
    public void saveSkuFullRelation(SkuFullReductionTO skuFullReductionTO) {
        Long skuId = skuFullReductionTO.getSkuId();

        // 封装满折优惠信息对象并保存
        if ((skuFullReductionTO.getDiscount().compareTo(BigDecimal.ZERO) == 1) &&
                skuFullReductionTO.getFullCount() > 0) {
            SmsSkuLadderEntity skuLadderEntity = new SmsSkuLadderEntity();
            skuLadderEntity.setSkuId(skuId);
            skuLadderEntity.setFullCount(skuFullReductionTO.getFullCount());
            skuLadderEntity.setDiscount(skuFullReductionTO.getDiscount());
            skuLadderEntity.setAddOther(skuFullReductionTO.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        // 封装满减优惠信息并保存
        if ((skuFullReductionTO.getFullPrice().compareTo(BigDecimal.ZERO) == 1) &&
                skuFullReductionTO.getReducePrice().compareTo(BigDecimal.ZERO) == 1) {
            SmsSkuFullReductionEntity skuFullReductionEntity = new SmsSkuFullReductionEntity();
            skuFullReductionEntity.setSkuId(skuId);
            skuFullReductionEntity.setFullPrice(skuFullReductionTO.getFullPrice());
            skuFullReductionEntity.setReducePrice(skuFullReductionTO.getReducePrice());
            skuFullReductionEntity.setAddOther(skuFullReductionTO.getPriceStatus());
            baseMapper.insert(skuFullReductionEntity);
        }

        // 批量保存会员优惠信息
        memberPriceService.saveMemberPriceBatch(skuId, skuFullReductionTO.getMemberPrice());

    }

}
